#include "FileSevice.h"
#include "../CinUserMap.h"
#include "../utils/Utils.h"


FileSevice::FileSevice(/* args */)
{
}

FileSevice::~FileSevice()
{
}


void FileSevice::doHandle(const char *pDataBuffer)
{
    NetPacket_File *pData = (NetPacket_File *)pDataBuffer;
    string uid = pData ->to;
    int tosd = CinUserMap::GetInstance()->findUserByUid(uid);
    if(tosd > 0){
        NetPacket_File vData;
        vData.csize = pData->csize;
        vData.filesize = pData->filesize;    
        vData.pidx = pData->pidx;
        vData.psize = pData->psize;
        strncpy(vData.to, pData->to, sizeof(vData.to));
        strncpy(vData.from, pData->from, sizeof(vData.from));
        strncpy(vData.filename, pData->filename, sizeof(vData.filename));
        strncpy(vData.fileid, pData->fileid, sizeof(vData.fileid));

        //strncpy(vData.data, pData->data, sizeof(vData.data));
        for(int i=0; i<pData->csize; i++){
            vData.data[i] = pData->data[i];
        }

        // cout <<"FileSevice vData.pidx:" << vData.pidx<< "  data size="<< sizeof(vData.data)<<endl;
        // std::string path = "/usr/local/wsock/";
        // path.append(vData.filename);

       // Utils::writeFile(path, vData.data, vData.csize);

        mSocket->send(tosd, NET_FILE, (const char*)&vData, sizeof(vData));
    }
    
    //free(pDataBuffer);
}

